# usplash - Userspace bootsplash utility
#
# usplash provides a boot splash screen on the system console using
# the kernel framebuffer device.  On boot, this is nominally started by
# the initramfs so the pre-start script, script and post-start script
# parts are actually not run.  These are normally run on shutdown instead.

description	"Userspace bootsplash utility"

start on (runlevel [016]
	  and (stopped gdm
	       or stopped kdm
	       or stopped xdm))
stop on (starting-dm
	 or stopped rc RUNLEVEL=[2345]
	 or starting rcS
	 or starting mountall-shell)

expect fork
kill timeout 10

pre-start script
    if [ "$UPSTART_EVENTS" = "stopped" ]
    then
	# TODO: this is ugly, but we don't seem to get RUNLEVEL in the event
	# environment for 'stopped gdm' etc.
	set -- $(runlevel)
	case $2 in
	2|3|4|5)
	    stop
	    exit 0
	    ;;
	esac
    fi

    SPLASH=false
    if [ -f /proc/cmdline ]; then
	for x in $(cat /proc/cmdline); do
	    case $x in
		nosplash*)
		    SPLASH=false
		    ;;
		splash*)
		    SPLASH=:
		    ;;
	    esac
	done
    fi

    $SPLASH || { stop; exit 0; }

    # Clean up the console before we switch to it, to avoid text flicker
    if [ -x /usr/bin/tput ]
    then
	tput -Tlinux reset > /dev/tty8 || true
    fi
end script

script
    # Send the progress bar the other way on shutdown
    [ "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" -o "$RUNLEVEL" = "6" ] && down="-d"

    exec /sbin/usplash $down -c -p --background --pidfile /var/run/usplash.pid
end script

post-start script
    # Append our PID to the list of processes that sendsigs won't kill,
    # to avoid flicker on shutdown
    if [ "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "6" ]; then
	cat /var/run/usplash.pid >> /var/run/sendsigs.omit
    fi
end script

pre-stop script
    # Clear VT 8 of any console messages
    clear >/dev/tty8 || true
end script
